﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class TanSoDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from TanSo");
        }

        public TanSo GetById(String maTanSo)
        {
            TanSo ts = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from TanSo where MaTanSo = '" + maTanSo + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    ts = new TanSo();
                    ts.MaTanSo = dataReader.GetString(0);
                    ts.Ten = dataReader.GetString(1);
                    return ts;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return ts;
        }

        public bool Insert(TanSo tanSo)
        {

            String sql = "INSERT INTO [QLVT].[dbo].[TanSo]([MaTanSo],[Ten])"
               + "VALUES('" + tanSo.MaTanSo+ "'," + tanSo.Ten + ")";
            return Update(sql);
        }

        public bool DeleteById(String matanso)
        {
            return Update("delete from TanSo where MaTanSo = '" + matanso + "'");
        }

        public bool UpdateById(TanSo ts)
        {
            String sql = "update TanSo "
                 + "set ten = " + ts.Ten + "";
            return Update(sql);
        }
    }
}
